//添加角色的js页面
$(function () {
    var selectCompany = $("#addRoleBank");//银行
    $.ajax({
        url: "getAllCompany",
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                var company = data.allBankName;//银行
                console.log(company);
                selectCompany.html("");
                //向银行select传入数据
                var options = "<option value=\"\">--请选择--</option>";
                for (var i = 0; i < company.length; i++) {
                    options += "<option value=\"" + company[i].id + "\">" + company[i].bankname + "</option>";
                }
                selectCompany.html(options);
            }
        }
    })
})

//获取所选择的权限，并添加
function saveAddRole() {
    var roleName = $("#roleName").val();
    if (roleName == "") {
        alert("请输入角色名称！")
        return;
    }
    var addRoleBank = $("#addRoleBank").val();
    if (addRoleBank == "") {
        alert("请选择所属银行！");
        return;
    }
    var description = $("#description").val();
    //------------------------------------------------------------------
    //存量模型
    var sidebar1 = $("#sidebar1 option:selected").val();
    if (sidebar1 == -1) {
        alert("请为该角色添加 *存量模型* 模块的使用权限！");
        return;
    }
    var sidebar11 = $("#sidebar1-1 option:selected").val();
    var sidebar12 = $("#sidebar1-2 option:selected").val();
    var sidebar13 = $("#sidebar1-3 option:selected").val();
    if (sidebar1 == 0) {//无权限
        sidebar11 = 0;
        sidebar12 = 0
        sidebar13 = 0;
    } else if (sidebar1 == 1) {//查询
        if (sidebar11 == -1 || sidebar12 == -1 || sidebar13 == -1) {
            if (sidebar11 == -1) {
                sidebar11 = 1;
            }
            if (sidebar12 == -1) {
                sidebar12 = 1;
            }
            if (sidebar13 == -1) {
                sidebar13 = 1;
            }
        } else {
            if (sidebar11 == -1) {
                sidebar11 = sidebar1;
            } else if (sidebar11 != -1) {
                sidebar11 = sidebar11;
            }
            if (sidebar12 == -1) {
                sidebar12 = sidebar1;
            } else if (sidebar12 != -1) {
                sidebar12 = sidebar13;
            }
            if (sidebar13 == -1) {
                sidebar13 = sidebar1;
            } else if (sidebar13 != -1) {
                sidebar13 = sidebar13;
            }
        }
    } else if (sidebar3 == 2) {//维护
        if (sidebar11 == -1 || sidebar12 == -1 || sidebar13 == -1) {
            if (sidebar11 == -1) {
                sidebar11 = 2;
            }
            if (sidebar12 == -1) {
                sidebar12 = 2;
            }
            if (sidebar13 == -1) {
                sidebar13 = 2;
            }
        } else {
            if (sidebar11 == -1) {
                sidebar11 = sidebar1;
            } else if (sidebar11 != -1) {
                sidebar11 = sidebar11;
            }
            if (sidebar12 == -1) {
                sidebar12 = sidebar1;
            } else if (sidebar12 != -1) {
                sidebar12 = sidebar12;
            }
            if (sidebar13 == -1) {
                sidebar13 = sidebar1;
            } else if (sidebar13 != -1) {
                sidebar13 = sidebar13;
            }
        }
    }
    /*if (sidebar1 == 0) {//如果存量模型权限为无权限，存量模型下的模块都无权限
        sidebar11 = 0;
        sidebar12 = 0;
        sidebar13 = 0;
        console.log(sidebar11 + "," + sidebar12 + "," + sidebar13);
    } else if (sidebar1 == 1) {//有查询权限，子模块只能进行查询或者没有权限
        if (sidebar11 != -1 || sidebar12 != -1 || sidebar13 != -1) {//如果选择了题库和组合管理的权限就按照选择的来，如果没选，题库和组合管理的权限就是存量模型的权限
            sidebar11 = 1;
            sidebar12 = 1;
            sidebar13 = 1;
            console.log("空空：" + sidebar11 + "," + sidebar12 + "," + sidebar13);
        } else {
            if (sidebar11 == -1 && sidebar12 != -1) {
                sidebar11 = 1;//查询
                sidebar12 = $("#sidebar1-2 option:selected").val();
                console.log(sidebar11 + "," + sidebar12);
            }
            if (sidebar11 != -1 && sidebar12 == -1) {
                sidebar11 = $("#sidebar1-1 option:selected").val();
                sidebar12 = 1;//查询
                console.log(sidebar11 + "," + sidebar12);
            }
            if (sidebar11 == -1 && sidebar12 == -1) {
                sidebar11 = 1;
                sidebar12 = 1;
                console.log(sidebar11 + "," + sidebar12);
            }
        }
    } else if (sidebar1 == 2) {//维护
        if (sidebar11 == -1 || sidebar12 == -1) {//如果选择了题库和组合管理的权限就按照选择的来，如果没选，题库和组合管理的权限就是存量模型的权限
            if (sidebar11 == -1 && sidebar12 != -1) {
                sidebar11 = 2;
                sidebar12 = $("#sidebar1-2 option:selected").val();
                console.log("22:" + sidebar11 + "," + sidebar12);
            }
            if (sidebar11 == -1 && sidebar12 != -1) {
                sidebar11 = $("#sidebar1-1 option:selected").val();
                sidebar12 = 2;
                console.log("33:" + sidebar11 + "," + sidebar12);
            }
            if (sidebar11 == -1 && sidebar12 == -1) {
                sidebar11 = 2;
                sidebar12 = 2;
                console.log("44:" + sidebar11 + "," + sidebar12);
            }
        } else {
            sidebar11 = $("#sidebar1-1 option:selected").val();
            sidebar12 = $("#sidebar1-2 option:selected").val();
            console.log("11:" + sidebar11 + "," + sidebar12);
        }
    }*/
    //------------------------------------------------------------------
    //增量模型
    var sidebar2 = $("#sidebar2 option:selected").val();
    if (sidebar2 == -1) {//未选择
        alert("请为该角色添加 *增量模型* 模块的使用权限！");
        return;
    }
    var sidebar21 = $("#sidebar2-1 option:selected").val();
    var sidebar22 = $("#sidebar2-1 option:selected").val();
    if (sidebar2 == 0) {//如果增量模型无权限，存量模型下的模块也无权限
        sidebar21 = 0;
        sidebar22 = 0;
        console.log("sidebar21：" + sidebar21 + "sidebar22: " + sidebar21);
    } else if (sidebar2 == 1) {//查询
        if (sidebar21 != -1 || sidebar22 != -1) {
            sidebar21 = 1;
            sidebar22 = 1;
            console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
        } else {
            if (sidebar21 == -1 && sidebar22 != -1) {
                sidebar21 = 1;//查询
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
            if (sidebar21 != -1 && sidebar22 == -1) {
                sidebar22 = 1;//查询
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
            if (sidebar21 == -1 && sidebar22 == -1) {
                sidebar21 = 1;
                sidebar22 = 1;
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
        }
    } else if (sidebar2 == 2) {//维护
        if (sidebar21 == -1 || sidebar22 == -1) {
            if (sidebar21 == -1 && sidebar22 != -1) {
                sidebar21 = 2;
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
            if (sidebar21 != -1 && sidebar22 == -1) {
                sidebar22 = 2;
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
            if (sidebar21 == -1 && sidebar22 == -1) {
                sidebar21 = 2;
                sidebar22 = 2;
                console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
            }
        } else {
            sidebar21 = sidebar21;
            sidebar22 = sidebar22;
            console.log("增量模型: sidebar21: " + sidebar21 + ",sidebar22: " + sidebar22);
        }
    }

    //------------------------------------------------------------------
    //客户管理
    var sidebar3 = $("#sidebar3 option:selected").val();
    if (sidebar3 == -1) {
        alert("请为该角色添加 *客户管理* 模块的使用权限！");
        return;
    }
    var sidebar31 = $("#sidebar3-1 option:selected").val();
    var sidebar32 = $("#sidebar3-1 option:selected").val();
    var sidebar33 = $("#sidebar3-1 option:selected").val();
    if (sidebar3 == 0) {//无权限
        sidebar31 = 0;
        sidebar32 = 0
        sidebar33 = 0;
    } else if (sidebar3 == 1) {//查询
        if (sidebar31 == -1 || sidebar32 == -1 || sidebar33 == -1) {
            if (sidebar31 == -1) {
                sidebar31 = 1;
            }
            if (sidebar32 == -1) {
                sidebar32 = 1;
            }
            if (sidebar33 == -1) {
                sidebar33 = 1;
            }
        } else {
            if (sidebar31 == -1) {
                sidebar31 = sidebar3;
            } else if (sidebar31 != -1) {
                sidebar31 = sidebar31;
            }
            if (sidebar32 == -1) {
                sidebar32 = sidebar3;
            } else if (sidebar32 != -1) {
                sidebar32 = sidebar33;
            }
            if (sidebar33 == -1) {
                sidebar33 = sidebar3;
            } else if (sidebar33 != -1) {
                sidebar33 = sidebar33;
            }
        }
    } else if (sidebar3 == 2) {//维护
        if (sidebar31 == -1 || sidebar32 == -1 || sidebar33 == -1) {
            if (sidebar31 == -1) {
                sidebar31 = 2;
            }
            if (sidebar32 == -1) {
                sidebar32 = 2;
            }
            if (sidebar33 == -1) {
                sidebar33 = 2;
            }
        } else {
            if (sidebar31 == -1) {
                sidebar31 = sidebar3;
            } else if (sidebar31 != -1) {
                sidebar31 = sidebar31;
            }
            if (sidebar32 == -1) {
                sidebar32 = sidebar3;
            } else if (sidebar32 != -1) {
                sidebar32 = sidebar32;
            }
            if (sidebar33 == -1) {
                sidebar33 = sidebar3;
            } else if (sidebar33 != -1) {
                sidebar33 = sidebar33;
            }
        }
    }


//------------------------------------------------------------------
//业务管理
    //客户管理
    var sidebar4 = $("#sidebar4 option:selected").val();
    if (sidebar4 == -1) {
        alert("请为该角色添加 *客户管理* 模块的使用权限！");
        return;
    }
    var sidebar41 = $("#sidebar4-1 option:selected").val();
    var sidebar42 = $("#sidebar4-2 option:selected").val();
    var sidebar43 = $("#sidebar4-3 option:selected").val();
    var sidebar44 = $("#sidebar4-4 option:selected").val();
    if (sidebar4 == 0) {//无权限
        sidebar41 = 0;
        sidebar42 = 0;
        sidebar43 = 0;
        sidebar44 = 0;
    } else if (sidebar4 == 1) {//查询
        if (sidebar41 == -1) {
            sidebar41 = 1;
        } else {
            sidebar41 = sidebar41;
        }
        if (sidebar42 == -1) {
            sidebar42 = 1;
        } else {
            sidebar42 = sidebar42;
        }
        if (sidebar43 == -1) {
            sidebar43 = 1;
        } else {
            sidebar43 = sidebar43;
        }
        if (sidebar44 == -1) {
            sidebar44 = 1;
        } else {
            sidebar44 = sidebar44;
        }
    } else if (sidebar3 == 2) {
        if (sidebar41 == -1 || sidebar42 == -1 || sidebar43 == -1 || sidebar44 == -1) {
            if (sidebar41 == -1) {
                sidebar41 = 2;
            }
            if (sidebar42 == -1) {
                sidebar42 = 2;
            }
            if (sidebar43 == -1) {
                sidebar43 = 2;
            }
            if (sidebar44 == -1) {
                sidebar44 = 2;
            }
        } else {
            if (sidebar41 == -1) {
                sidebar41 = sidebar4;
            } else if (sidebar41 != -1) {
                sidebar41 = sidebar41;
            }
            if (sidebar42 == -1) {
                sidebar42 = sidebar4;
            } else if (sidebar42 != -1) {
                sidebar42 = sidebar42;
            }
            if (sidebar43 == -1) {
                sidebar43 = sidebar4;
            } else if (sidebar43 != -1) {
                sidebar43 = sidebar43;
            }
            if (sidebar44 == -1) {
                sidebar44 = sidebar4;
            } else if (sidebar44 != -1) {
                sidebar44 = sidebar44;
            }
        }
    }

//------------------------------------------------------------------
//系统管理
    var sidebar5 = $("#sidebar5 option:selected").val();
    if (sidebar5 == -1) {//未选择
        alert("请为该角色添加 *系统管理* 模块的使用权限！");
        return;
    }
    var sidebar51 = $("#sidebar5-1 option:selected").val();
    var sidebar52 = $("#sidebar5-2 option:selected").val();
    if (sidebar5 == 0) {//如果增量模型无权限，存量模型下的模块也无权限
        sidebar51 = 0;
        sidebar52 = 0;
        console.log("sidebar51：" + sidebar51 + "sidebar52: " + sidebar51);
    } else if (sidebar5 == 1) {//查询
        if (sidebar51 == -1 || sidebar22 == -1) {//如果都为-1, 权限都为维护权限
            sidebar51 = 1;
            sidebar52 = 1;
            console.log("增量模型: sidebar51: " + sidebar51 + ",sidebar52: " + sidebar52);
        } else {
            if (sidebar51 == -1 && sidebar52 != -1) {
                sidebar51 = 1;//查询
                sidebar52 = $("#sidebar5-2 option:selected").val();
                console.log("增量模型: sidebar51: " + sidebar51 + ",sidebar52: " + sidebar52);
            }
            if (sidebar51 != -1 && sidebar52 == -1) {
                sidebar51 = $("#sidebar5-1 option:selected").val();
                sidebar52 = 1;//查询
                console.log("增量模型: sidebar51: " + sidebar51 + ",sidebar52: " + sidebar52);
            }
            if (sidebar51 == -1 && sidebar52 == -1) {
                sidebar51 = 1;
                sidebar52 = 1;
                console.log("增量模型: sidebar51: " + sidebar51 + ",sidebar52: " + sidebar52);
            }
        }
    } else if (sidebar5 == 2) {//维护
        if (sidebar51 == -1 || sidebar52 == -1) {
            if (sidebar51 == -1) {
                sidebar51 = 2;
            }
            if (sidebar52 == -1) {
                sidebar52 = 2;
            }
        } else {
            sidebar51 = $("#sidebar5-1 option:selected").val();
            sidebar52 = $("#sidebar5-2 option:selected").val();
            console.log("增量模型: sidebar51: " + sidebar51 + ",sidebar52: " + sidebar52);
        }
    }

//------------------------------------------------------------------
    console.log("---------------------------------------------------------------")
    /*角色管理*/
    console.log(roleName + "，" + addRoleBank + "，" + description);
    /*职责权限管理*/
    console.log(sidebar1 + "，" + sidebar11 + "，" + sidebar12);
    console.log(sidebar2 + "，" + sidebar21 + "，" + sidebar22);
    console.log(sidebar3 + "，" + sidebar31 + "，" + sidebar32 + "，" + sidebar33);
    console.log(sidebar4 + "，" + sidebar41 + "，" + sidebar42 + "，" + sidebar43 + "，" + sidebar44);
    console.log(sidebar5 + "，" + sidebar51 + "，" + sidebar52);
    console.log(sidebar1 + sidebar11 + sidebar12 +sidebar13+ sidebar2 + sidebar21 + sidebar22 +
        sidebar3 + sidebar31 + sidebar32 + sidebar33 + sidebar4 + sidebar41 + sidebar42 + sidebar43 + sidebar5 + sidebar5 + sidebar52)
    $.ajax({
        url: "addRoleInfo",
        type: "post",
        data: {
            rooname: roleName,
            bankid: addRoleBank,
            description: description,
            rolePermission: sidebar1 + sidebar11 + sidebar12 + sidebar13+sidebar2 + sidebar21 + sidebar22 +
                sidebar3 + sidebar31 + sidebar32 + sidebar33 + sidebar4 + sidebar41 + sidebar42 +
                sidebar43 + sidebar5 + sidebar5 + sidebar52,
        },
        success: function (data) {
            if (data.addRoleResult == "角色添加成功") {
                alert("角色添加成功！");
                loadPage("systemManager/roleManager.html");
            } else {
                alert("角色添加失败！");
            }
        },
        error: function (data) {
            alert("error");
        }
    })
}

//校验角色名是否重复
function verifyRoleName() {
    var roleName = $("#roleName").val();
    if (roleName == "") {
        $("#rolaNameMsg").html("角色名不能为空!")
        return;
    }
    $.ajax({
        url: "verifyRoleName",
        type: "post",
        data: {
            rooname: roleName,
        },
        success: function (data) {
            if (data.validateRoleName == "可用") {
                $("#rolaNameMsg").html("角色名可用!")
            } else {
                $("#rolaNameMsg").html("角色名已占用!")
            }
        },
        error: function (data) {
            alert("error");
        }
    })
}

//收起功能 ---↓↓↓↓---
document.getElementById("bt1").onclick = function () {
    var bt1 = document.getElementById("bt1");
    var thebt1 = document.getElementById("thebt1");
    var temp = 0; //0为显示
    bt1.onclick = function () {
        if (temp == 0) {
            thebt1.style = "display:none";
            temp = 1;
        } else {
            thebt1.style = "display:inline-block";
            temp = 0;
        }
    }
}
document.getElementById("bt2").onclick = function () {
    var bt2 = document.getElementById("bt2");
    var thebt2 = document.getElementById("thebt2");
    var temp = 0; //0为显示
    bt2.onclick = function () {
        if (temp == 0) {
            thebt2.style = "display:none";
            temp = 1;
        } else {
            thebt2.style = "display:inline-block";
            temp = 0;
        }
    }
}
document.getElementById("bt3").onclick = function () {
    var bt3 = document.getElementById("bt3");
    var thebt3 = document.getElementById("thebt3");
    var temp = 0; //0为显示
    bt3.onclick = function () {
        if (temp == 0) {
            thebt3.style = "display:none";
            temp = 1;
        } else {
            thebt3.style = "display:inline-block";
            temp = 0;
        }
    }
}
document.getElementById("bt4").onclick = function () {
    var bt4 = document.getElementById("bt4");
    var thebt4 = document.getElementById("thebt4");
    var temp = 0; //0为显示
    bt4.onclick = function () {
        if (temp == 0) {
            thebt4.style = "display:none";
            temp = 1;
        } else {
            thebt4.style = "display:inline-block";
            temp = 0;
        }
    }
}
document.getElementById("bt5").onclick = function () {
    var bt5 = document.getElementById("bt5");
    var thebt5 = document.getElementById("thebt5");
    var temp = 0; //0为显示
    bt5.onclick = function () {
        if (temp == 0) {
            thebt5.style = "display:none";
            temp = 1;
        } else {
            thebt5.style = "display:inline-block";
            temp = 0;
        }
    }
}